/*
 * @Author: llch
 * @Date: 2020-08-05 15:22:31
 * @LastEditTime: 2020-08-12 10:52:28
 * @LastEditors: Please set LastEditors
 * @Description: 格子对象
 * @FilePath: \shici_demo\assets\Script\game\obj\tile.ts
 */

import { Level } from "./level";
import { Idiom } from "./idiom";
import { Poem } from "./poem";

const {ccclass, property} = cc._decorator;

@ccclass
export class Tile extends cc.Component {

    public tileId: number = undefined;              // 格子id
    public isUsed: boolean = false;                 // 格子使用状态     筛选棋盘中显示的棋子
    public char: string = undefined;                // 格子上的字符
    public data:Array<string> = [];                 // 格子上保存的词语，诗句对象
    public isSpace: boolean = false;                // 格子是否为 去 字状态      为true表明 这个位置 是空位
    public hasWord: boolean = false;                // 空位格子上是否有 字
    public isSucceed: boolean = false;              // 格子填词是否正确
    public idioms: Idiom = undefined;               // 成语对象数组
    public poem: Poem = undefined;                  // 诗词对象
    public currentId: number = undefined;           // 当前上挂载的词id

    @property(cc.Sprite)
    public bgSprite: cc.Sprite = null;
    @property(cc.Label)
    public wordLabel: cc.Label = null;

    @property({                                      // 格子默认背景
        type: cc.SpriteFrame,
    })
    public tile_bg: cc.SpriteFrame = null;
    @property({                                      // 格子 去 字状态 背景
        type: cc.SpriteFrame,
    })
    public tile_bg_normal: cc.SpriteFrame = null;
    @property({                                      // 格子选择背景
        type: cc.SpriteFrame,
    })
    public tile_bg_selected: cc.SpriteFrame = null;
    @property({                                      // 格子正确背景
        type: cc.SpriteFrame,
    })
    public tile_bg_success: cc.SpriteFrame = null;
    @property({                                      // 格子默认背景2
        type: cc.SpriteFrame,
    })
    public tile_bg_normal2: cc.SpriteFrame = null;
    @property({                                      // 选择区格子 背景
        type: cc.SpriteFrame,
    })
    public tile_select_bg: cc.SpriteFrame = null;

    // 还原
    public restTile(): void {
        this.isUsed = false;
        this.char = "";
        this.data = [];
        this.idioms = undefined;
        this.poem = undefined;
        this.isSpace = false;
        this.isSucceed = false;
        this.currentId = undefined;
        this.hasWord = false;

        this.bgSprite.spriteFrame = this.tile_bg;
        this.wordLabel.string = "";
        this.node.opacity = 0;
    }

    // 设置
    public setTile(isUsed: boolean, char: string, data: Array<string>): void {
        this.isUsed = isUsed;
        this.char = char;
        if(data) {
            this.data = data;
        }
        this.node.opacity = 255;
    }

    // 修改bg
    /**
     * @description: 
     * @param {number} 0 tile_bg, 1 tile_bg_normal, 2 tile_bg_selected, 3 tile_select_bg, 4 tile_bg_success, 5 tile_bg_normal2
     * @return {type} 
     */
    public setBgSprite(num: number): void {
        switch(num) {
            case -1:
                this.bgSprite.spriteFrame = null;
                break;
            case 0:
                this.bgSprite.spriteFrame = this.tile_bg;
                break;
            case 1:
                this.bgSprite.spriteFrame = this.tile_bg_normal;
                break;
            case 2:
                this.bgSprite.spriteFrame = this.tile_bg_selected;
                break;
            case 3:
                this.bgSprite.spriteFrame = this.tile_select_bg;
                break;
            case 4:
                this.bgSprite.spriteFrame = this.tile_bg_success;
                break;
            case 5:
                this.bgSprite.spriteFrame = this.tile_bg_normal2;
                break;
            default:
                this.bgSprite.spriteFrame = this.tile_bg;
        }
    }

    // 修改charLabel
    public setWordLabel(str: string): void {
        this.wordLabel.string = str;
    }

    // 添加监听事件
    public addListener(fn: Function, target: object) {
        this.node.on(cc.Node.EventType.TOUCH_END, fn, target);
    }

    // 移除监听事件
    public removeListener(fn: Function, target: object) {
        this.node.off(cc.Node.EventType.TOUCH_END, fn, target);
    }

}
